package ui.panel
{

	import com.greensock.TweenLite;

	import flash.display.Bitmap;
	import flash.geom.Point;

	import globals.Global;

	import manager.LinkManager;

	import ui.component.Button;
	import ui.panel.cell.OptionCell;

	/**
	 * 塔楼操作面板
	 */
	public class OptionPanel extends BasePanel
	{
		private var buildCell:OptionItem;
		private var optionPoint:Point;
		private var optionConfig:Array;
		private var _btnItems:Array;

		public function OptionPanel()
		{
			super();
			pid = PanelType.OPTION_PANEL;
		}

		override protected function initUI():void
		{
			_bg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("buildTower_ring"));
			_bg.x = -(_bg.width * 0.5);
			_bg.y = -(_bg.height * 0.5);
			this.addChildAt(_bg, 0);

			optionConfig = new Array(); //下右上左
			optionConfig[0] = ["拆除", "sc_distroy", destroyHandler];
			optionConfig[1] = ["建造", "sc_build", buildHandler];
			optionConfig[2] = ["派驻", "sc_presence", presenceHandler]; //派驻
			optionConfig[3] = ["查看", "sc_bag", lookInfoHandler];
			_btnItems = [];
		}


		private function destroyHandler(item:OptionItem):void
		{
			close();
		}

		private function buildHandler(item:OptionItem):void
		{
			Global.uiManager.openPanel(PanelType.TOWER_PANEL);
			Global.uiManager.towerPanel.buildPoint = optionPoint;
			close();
		}

		private function presenceHandler(item:OptionItem):void
		{
			Global.uiManager.warriorPanel.isDispatch = true;
			LinkManager.getHeroList();
			close();
		}

		private function lookInfoHandler(item:OptionItem):void
		{
			close();
		}

		private function upgradeHandler():void
		{
		}

		public function openByPoint(point:Point):void
		{
			this.x = point.x;
			this.y = point.y;
			optionPoint = point;
			//Global.uiManager.showMaskLayer();
			this.open(false);
			animateShow();

			var gap:uint = 360 / optionConfig.length;
			var radius:uint = 60;

			for(var i:int = 0; i < optionConfig.length; i++)
			{
				var data:Array = optionConfig[i];

				if(data != null)
				{
					var item:OptionItem = new OptionItem(data[0], data[1], data[2]);
					item.x = Math.cos(Math.PI * (90 + gap * i) / 180) * radius;
					item.y = Math.sin(Math.PI * (90 + gap * i) / 180) * radius;
					this.addChild(item);
					_btnItems.push(item);
				}
			}
		}

		override public function close():void
		{
			super.close();
			animateHide();
			//Global.uiManager.hideMaskLayer();
		}

		public function animateShow():void
		{
			scaleX = 0
			scaleY = 0;
			TweenLite.to(this, 0.3, {scaleX: 1, scaleY: 1});
		}

		public function animateHide():void
		{
			TweenLite.to(this, 0.3, {scaleX: 0, scaleY: 0});
		}

		override public function clear():void
		{

		}
	}
}

import flash.display.Bitmap;
import flash.display.BitmapData;

import globals.Global;

import ui.component.Button;
import ui.component.Label;

import util.TextStyle;

class OptionItem extends Button
{
	private var icon:Bitmap;
	private var operationName:Label;

	public function OptionItem(name:String, iconUrl:String, handler:Function)
	{
		super(Global.assetsManager.getAssetsAsBitmapData("buildTower_btn_default"), Global.assetsManager.getAssetsAsBitmapData("buildTower_btn_over"),
			null, null, handler);

		skin.x = -(this.width * 0.5);
		skin.y = -(this.height * 0.5);

		icon = new Bitmap(Global.assetsManager.getAssetsAsBitmapData(iconUrl));
		icon.x = skin.x + (skin.width - icon.width) * 0.5;
		icon.y = skin.y + (skin.height - icon.height) * 0.5;
		this.addChild(icon);

		var operationName:Label = new Label(12, 0xFFFFFF, false, "left", true, 0x1f1200);
		operationName.text = name;
		operationName.x = skin.x + 20;
		operationName.y = skin.y + 37;
		this.addChild(operationName);
	}
}